package org.strongme.modules.project.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.strongme.common.persistence.Page;
import org.strongme.common.web.BaseController;
import org.strongme.modules.project.entity.Project;
import org.strongme.modules.project.entity.SubProject;
import org.strongme.modules.project.service.ProjectService;
import org.strongme.util.StringUtils;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Controller
@RequestMapping("project/project")
public class ProjectController extends BaseController{
	
	@Autowired
	private ProjectService projectService;
	
	@ModelAttribute("project")
	public Project get(@RequestParam(required=false) String id) {
		Project project = null;
		if (StringUtils.isNotBlank(id)){
			project = projectService.get(new Project(id));
		}else{
			project = new Project();
		}
		return project;
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Project project, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Project> page = projectService.findPage(new Page<Project>(request, response), project); 
        model.addAttribute("page", page);
		return "modules/project/project/projectList.jsp";
	}
	
	@RequestMapping(value = "form")
	public String form(Project project, Model model) {
		model.addAttribute("project", project);
		return "modules/project/project/projectForm.jsp";
	}
	
	@RequestMapping(value = "save")
	public String save(Project project, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, project)){
			return form(project, model);
		}
		projectService.save(project);
		addMessage(redirectAttributes, "保存工程'" + project.getName() + "'成功");
		return "redirect:/project/project";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Project project, RedirectAttributes redirectAttributes) {
		projectService.delete(project);
		addMessage(redirectAttributes, "删除工程成功");
		return "redirect:/project/project";
	}
	
	@ResponseBody
	@RequestMapping(value = "treeData")
	public List<Map<String, Object>> treeData(@RequestParam(required=false) String extId, @RequestParam(required=false) String type,
			@RequestParam(required=false) Long grade, @RequestParam(required=false) Boolean isAll, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Project> list = projectService.findList(new Project());
		Map<String,Object> mapRoot = Maps.newHashMap();
		mapRoot.put("id", 0);
		mapRoot.put("pId", "-1");
		mapRoot.put("pIds", "0");
		mapRoot.put("name", "所有工程");
		mapRoot.put("isParent", true);
		mapList.add(mapRoot);
		for (int i=0; i<list.size(); i++){
			Project e = list.get(i);
			if ((StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId())))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", 0);
				map.put("pIds", "0,");
				map.put("suoxie", e.getSuoxie());
				map.put("name", e.getName());
				map.put("isParent", false);
				mapList.add(map);
			}
		}
		return mapList;
	}
	
	@ResponseBody
	@RequestMapping(value = "subTreeData")
	public List<Map<String, Object>> subTreeData(@RequestParam(required=false) String extId, @RequestParam(required=false) String type,
			@RequestParam(required=false) Long grade, @RequestParam(required=false) Boolean isAll, HttpServletResponse response) {
		List<Map<String, Object>> mapList = Lists.newArrayList();
		List<Project> list = projectService.findList(new Project());
		Map<String,Object> mapRoot = Maps.newHashMap();
		mapRoot.put("id", 0);
		mapRoot.put("pId", "-1");
		mapRoot.put("pIds", "0");
		mapRoot.put("name", "所有工程");
		mapRoot.put("type", "root");
		mapRoot.put("isParent", true);
		mapList.add(mapRoot);
		for (int i=0; i<list.size(); i++){
			Project e = list.get(i);
			if ((StringUtils.isBlank(extId) || (extId!=null && !extId.equals(e.getId())))){
				Map<String, Object> map = Maps.newHashMap();
				map.put("id", e.getId());
				map.put("pId", 0);
				map.put("pIds", "0,");
				map.put("name", e.getName());
				map.put("type", "project");
				if(e.getSubProjectList().size()>0) {
					Map<String, Object> subMap = Maps.newHashMap();
					for(SubProject sp : e.getSubProjectList()) {
						subMap = Maps.newHashMap();
						subMap.put("id", sp.getId());
						subMap.put("pId", e.getId());
						subMap.put("pIds", "0,"+e.getId());
						subMap.put("name", sp.getName());
						subMap.put("suoxiePro", e.getSuoxie());
						subMap.put("suoxieSub", sp.getSuoxie());
						subMap.put("type", "subProject");
						subMap.put("isParent", false);
						mapList.add(subMap);
					}
					map.put("isParent", true);
				}else {
					map.put("isParent", false);
				}
				mapList.add(map);
			}
		}
		return mapList;
	}

}
